package com.whmskj.xjlhsc.pc.productionreport.task.yieldinquiry;

import com.whmskj.xjlhsc.pc.productionreport.task.mapper.ProductionMapper;
import com.whmskj.xjlhsc.pc.productionreport.task.pojo.DayVO;
import com.whmskj.xjlhsc.pc.productionreport.task.pojo.YieldInquiryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * @author jiaruchun
 * @date 2025-09-17 15:47
 * @Description: 产量查询
 */
@Slf4j
@Component
@EnableScheduling
public class YieldInquiryTask {

    @Autowired
    private ProductionMapper productionMapper;

    //每天早上10点执行
    @Scheduled(cron = "0 0 10 * * ?")
    //每隔5秒执行
//    @Scheduled(cron = "0/5 * * * * ?")
    public void yieldInquiry(){
        List<YieldInquiryVO> yieldInquiry = productionMapper.getYieldInquiry();
        for (YieldInquiryVO yieldInquiryVO : yieldInquiry) {
            //小数保留两位
            yieldInquiryVO.setEfficiency((Math.round(yieldInquiryVO.getEfficiency() * 10000) / 10000f) > 100?100 : (Math.round(yieldInquiryVO.getEfficiency() * 10000) / 10000f));
            yieldInquiryVO.setEnergyConsumptionPerKg(Math.round(yieldInquiryVO.getEnergyConsumptionPerKg() * 10000) / 10000f);
            yieldInquiryVO.setYieldKg(Math.round(yieldInquiryVO.getYieldKg() * 10000) / 10000f);
            yieldInquiryVO.setYieldKm(Math.round(yieldInquiryVO.getYieldKm() * 10000) / 10000f);
            //能耗保留两位小数
            yieldInquiryVO.setEnergyConsumption(Math.round(yieldInquiryVO.getEnergyConsumption() * 100) / 100f);
            if(yieldInquiryVO.getShift().equals("晚班")){
                yieldInquiryVO.setStartTime("21:30");
                yieldInquiryVO.setEndTime("09:30");
            }
        }
        if (productionMapper.getDayProductionDataByyieldInquiry1(LocalDate.now().minusDays(1).toString()) == 0) {
            productionMapper.insertYieldInquiry(yieldInquiry);
        }
    }

    //每天晚上10点执行
    @Scheduled(cron = "0 0 22 * * ?")
    //每隔5秒执行
//    @Scheduled(cron = "0/5 * * * * ?")
    public void yieldInquiryWan(){
        List<YieldInquiryVO> yieldInquiry = productionMapper.getYieldInquiryWan();
        for (YieldInquiryVO yieldInquiryVO : yieldInquiry) {
            //小数保留两位
            yieldInquiryVO.setEfficiency((Math.round(yieldInquiryVO.getEfficiency() * 10000) / 10000f) > 100?100 : (Math.round(yieldInquiryVO.getEfficiency() * 10000) / 10000f));
            yieldInquiryVO.setEnergyConsumptionPerKg(Math.round(yieldInquiryVO.getEnergyConsumptionPerKg() * 10000) / 10000f);
            yieldInquiryVO.setYieldKg(Math.round(yieldInquiryVO.getYieldKg() * 10000) / 10000f);
            yieldInquiryVO.setYieldKm(Math.round(yieldInquiryVO.getYieldKm() * 10000) / 10000f);
            //能耗保留两位小数
            yieldInquiryVO.setEnergyConsumption(Math.round(yieldInquiryVO.getEnergyConsumption() * 100) / 100f);
            if(yieldInquiryVO.getShift().equals("晚班")){
                yieldInquiryVO.setStartTime("21:30");
                yieldInquiryVO.setEndTime("09:30");
            }
        }
        String string = LocalDate.now().toString();
        Integer b = productionMapper.getDayProductionDataByyieldInquiry2(string);
        if (b == 0) {
            productionMapper.insertYieldInquiry(yieldInquiry);
        }
    }

}
